var files = [], pname
//选择文件后
$(document).on('change', '#uploadimages', function(){
    for(i=0;i<this.files.length;i++){
        files.push(this.files[i])
    }
    var uploader = $('ul.weui_uploader_files')
    uploader.empty()
    for(i=0;i<files.length;i++){  
        var url = URL.createObjectURL(files[i])
        uploader.append('<li class="weui_uploader_file img_wait"'
        +' style="background-image:url('+url+')"></li>')
    }
    refreshcount()
    console.info('change')
})

//上传键
$('#uploadform').submit(function(e){
    e.preventDefault();
    if(files.length>0){
        upload(0)
    }else{
        mymessage2('无法上传', '请先添加上传文件后方可上传')
    }
})
//上传过程
upload = function(i){
    var formdata = new FormData()
    formdata.append('imgfile', files[i])
    formdata.append('pname', $('#pname').val())
    var status = $('<div class="weui_uploader_status_content">0%</div>')
    var progress = $('<div class="weui_progress" style="margin-top: 79px;">'+
        '<div class="weui_progress_bar">'+
            '<div class="weui_progress_inner_bar" style="width: 0%;"></div>'+
        '</div>'+
    '</div>')
    xhr = new XMLHttpRequest()
    xhr.onreadystatechange = function(){
        if(xhr.readyState==4){
            // status.text('100%')
            status.html('<i class="weui_icon_success"></i>')
            status.parents('li').removeClass('img_wait')
            refreshcount()
            console.info('ReadyState==4:'+xhr.responseText)
            if(i<files.length-1){
                upload(i+1)
            }else{
                $('#uploadbtn').removeClass('weui_btn_disabled').text('继续上传')
                myloadinghide()
                mymessage2('图片上传操作成功', '<i class="weui_icon_success weui_icon_msg"></i><br/>点击确定键返回')
                files = []
            }
        }
    }
    xhr.upload.addEventListener('loadstart', function(e){
        $('li.weui_uploader_file').eq(i).append(status).append(progress).addClass('weui_uploader_status')
        $('#uploadbtn').addClass('weui_btn_disabled')
        myloadingshow('上传图片中...')
        console.info('Starting....')
    }, false);   
    xhr.upload.addEventListener('progress', function(e){
        var percent = Math.ceil((e.loaded/e.total).toFixed(2) * 100)+'%'
        status.text(percent)
        progress.find('.weui_progress_inner_bar').css('width', percent)
        console.info(e.loaded, e.total)
    }, false);
    xhr.upload.addEventListener('load', function(e){
        myloadinghide()
        console.info('Loaded...')
    }, false);        
    xhr.upload.addEventListener('error', function(e){
        status.html('<i class="weui_icon_warn"></i>')
        console.info(e)
    }, false);    
    xhr.open('POST', 'manage?action=sellpostimage')
    xhr.send(formdata)
}
//弹出确定菜单
mymessage2 = function(title, content){
    var dailog = $('#dialog2')
    dailog.find('.weui_dialog_title').text(title)
    dailog.find('.weui_dialog_bd').html(content)
    dailog.find('.weui_btn_dialog').click(function(){
        dailog.hide()
    })
    dailog.show()
}
//弹出确实或取消菜单
mymessage1 = function(title, content, sucfunc){
    var dailog = $('#dialog1')
    dailog.find('.weui_dialog_title').text(title)
    dailog.find('.weui_dialog_bd').html(content)
    dailog.find('.weui_btn_dialog.default').click(function(){
        dailog.hide()
    })
    dailog.find('.weui_btn_dialog.primary').unbind('click').click(sucfunc)        
    dailog.show()
}
//弹出输入菜单
myinputdailog = function(title, content, sucfunc){
    var dailog = $('#dialog1')
    dailog.find('.weui_dialog_title').text(title)
    dailog.find('.weui_dialog_bd').html('<input class="weui_input" type="tel" placeholder="'+content+'">')
    dailog.find('.weui_btn_dialog.default').click(function(){
        dailog.hide()
    })
    dailog.find('.weui_btn_dialog.primary').unbind('click').click(sucfunc)        
    dailog.show()    
}
//img_wait图片点击删除确认
$(document).on('click', '.img_wait', function(){
    var self = $(this)
    deletefunc = function(){
        files.splice(self.index(), 1)
        self.remove()
        $('#dialog1').hide()
        refreshcount()
    }
    mymessage1('删除图片确认', '您确定要取消本张图片的上传?<br/>按确定键确认,按取消键返回', deletefunc)
})
//更新图片数目
refreshcount = function(){
    var total = files.length
    var curent = total - $('.img_wait').length
    $('.weui_cell_ft').text(curent + '/'+ total)
}
//设置disable键无效
$(document).on('click', '.weui_btn_disabled', function(){
    return false;
})
//加载Toast
myloadingshow = function(title){
    var toast = $('#loadingToast')
    toast.find('.weui_toast_content').text(title)
    toast.show()
}
myloadinghide = function(){$('#loadingToast').hide()}